package code.example.hystrix.test.annotaion;

import code.example.tool.LogFactory;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheKey;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.concurrent.TimeUnit;


public class HystrixCommandVisitHttpWithAnnotation {
    private  String url="http://www.baidu.com";

            @HystrixCommand(
                    fallbackMethod = "visitBaiDuFallback",
                    threadPoolProperties = {
                        @HystrixProperty(name = "coreSize", value = "30"),
                        @HystrixProperty(name = "maxQueueSize", value = "100"),
                        @HystrixProperty(name = "queueSizeRejectionThreshold", value = "20") },
                    commandProperties = {
                        @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "500"),
                        @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "50")
            })
            @CacheResult()
    public  String visitBaiDu(@CacheKey  String userId) throws Exception {
            HttpGet httpGet = new HttpGet(url);
            HttpClient httpClient = HttpClients.createDefault();
            HttpResponse httpResponse = httpClient.execute(httpGet);
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            ByteArrayOutputStream byteArrayOutputStream = (new ByteArrayOutputStream());
            httpResponse.getEntity().writeTo(byteArrayOutputStream);
            String result = new String(byteArrayOutputStream.toByteArray());
            LogFactory.getLog().info(Thread.currentThread().getName()+": finish:"+result);
            try {
                TimeUnit.SECONDS.sleep(2);
            }catch (Exception e){};
            return result;
    }

    public String visitBaiDuFallback(String userId){
                return "<html><body>userId:"+userId+"</body></html>";
    }

    public static void main(String args []){
                String userId = "9001";
         try {
             (new HystrixCommandVisitHttpWithAnnotation()).visitBaiDu(userId);
         }catch (Exception e){
             LogFactory.getLog().error(e);
         }
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        HystrixCommandVisitHttpWithAnnotation hystrixCommandVisitHttpWithAnnotation = (HystrixCommandVisitHttpWithAnnotation)applicationContext.getBean("hystrixCommandVisitHttpWithAnnotation");
        HystrixRequestContext context = null;
        try {
             context = HystrixRequestContext.initializeContext();
             String result = hystrixCommandVisitHttpWithAnnotation.visitBaiDu(userId);
             LogFactory.getLog().info("visitBaiDu :"+result);
            result =hystrixCommandVisitHttpWithAnnotation.visitBaiDu(userId);
            LogFactory.getLog().info("visitBaiDu :"+result);

        }catch (Exception e){
            LogFactory.getLog().error(e);
        }finally {
            context.shutdown();
        }

    }
}
